﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using MagicBag.CRM.Model;
using MagicBag.CRM.BLL;
namespace MagicBag.CRM.Service
{
    public class CommonDictionaryService : BaseService
    {
        /// <summary>
        /// 获取所有字典数据
        /// </summary>
        /// <param name="context"></param>
        public void GetCommonDictionaries(HttpContext context)
        {
            string category = context.Request["Category"];
            string keyWords = context.Request["KeyWords"];

            int pageIndex;
            Int32.TryParse(context.Request["page"], out pageIndex);

            int pageSize;
            Int32.TryParse(context.Request["rows"], out pageSize);

            CommonDictionaryBLL cdBLL = new CommonDictionaryBLL();

            IList<CommonDictionary> list = cdBLL.GetCommonDictionaries();// new List<CommonDictionary>();

            #region 查询
            if (!string.IsNullOrEmpty(category))
            {
                if (category == "Category")
                    list = list.Where(item => item.Category.Contains(keyWords)).ToList();
                else if (category == "Name")
                    list = list.Where(item => item.Name.Contains(keyWords)).ToList();
                else if (category == "Value")
                    list = list.Where(item => item.Value.Contains(keyWords)).ToList();
                else if (category == "Description")
                    list = list.Where(item => item.Description.Contains(keyWords)).ToList();
            }
            else if (string.IsNullOrEmpty(category) && !string.IsNullOrEmpty(keyWords))
            {
                list = list.Where(item => item.Category.Contains(keyWords) || item.Name.Contains(keyWords) || item.Value.Contains(keyWords) || item.Description.Contains(keyWords)).ToList();
            }
            #endregion

            int total = list.Count;
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            string s = GetEasyUIDetailJson<CommonDictionary>(list, total);
            ResponseJson(context, s);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="context"></param>
        public void Insert(HttpContext context)
        {
            CommonDictionary dic = new CommonDictionary();
            int id;
            Int32.TryParse(context.Request["ID"], out id);
            dic.ID = id;
            dic.Category = context.Request["Category"];
            dic.Name = context.Request["Name"];
            dic.Value = context.Request["Value"];
            dic.Description = context.Request["Description"];
            int pid;
            Int32.TryParse(context.Request["ParentID"], out pid);
            dic.ParentID = pid;

            Result result = new Result();
            result.Success = true;
            try
            {
                CommonDictionaryBLL cdBLL = new CommonDictionaryBLL();
                cdBLL.Insert(dic);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="context"></param>
        public void Update(HttpContext context)
        {

            CommonDictionary dic = new CommonDictionary();
            int id;
            Int32.TryParse(context.Request["ID"], out id);
            dic.ID = id;
            dic.Category = context.Request["Category"];
            dic.Name = context.Request["Name"];
            dic.Value = context.Request["Value"];
            dic.Description = context.Request["Description"];
            int pid;
            Int32.TryParse(context.Request["ParentID"], out pid);
            dic.ParentID = pid;

            Result result = new Result();
            result.Success = true;
            try
            {
                CommonDictionaryBLL cdBLL = new CommonDictionaryBLL();
                cdBLL.Update(dic);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="context"></param>
        public void Delete(HttpContext context)
        {
            Result result = new Result();
            result.Success = true;

            int id;
            Int32.TryParse(context.Request["id"], out id);

            try
            {
                CommonDictionaryBLL cdBLL = new CommonDictionaryBLL();
                cdBLL.Delete(id);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        public void GetCategories(HttpContext context)
        {
            CommonDictionaryBLL cdBLL = new CommonDictionaryBLL();
            IList<CommonDictionary> list = cdBLL.GetCategories();// new List<CommonDictionary>();

            //添加默认分类
            CommonDictionary defualtCategory = new CommonDictionary();
            defualtCategory.ID = 0;
            defualtCategory.Name = "";
            list.Insert(0, defualtCategory);

            string s = JsonHelper.GetJson<IList<CommonDictionary>>(list);

            ResponseJson(context, s);
        }

    }
}
